cmake_minimum_required(VERSION 3.15)
project(test_package LANGUAGES CXX)

find_package(OpenSceneGraph REQUIRED CONFIG)
add_executable(${PROJECT_NAME} test_package.cpp)
target_link_libraries(${PROJECT_NAME} OpenSceneGraph::OpenSceneGraph)

get_directory_property(compile_definitions DIRECTORY ${CMAKE_SOURCE_DIR} COMPILE_DEFINITIONS)
foreach(compile_definition ${compile_definitions})
    if("${compile_definition}" MATCHES "(WITH_.+)=(1)$")
        string(REPLACE "=" ";" definition_list ${compile_definition})
        list(GET definition_list 0 definition_key)
        list(GET definition_list 1 definition_value)
        message("Defined: ${definition_key}=${definition_value}")
        if("${definition_key}" STREQUAL "WITH_BMP")
            target_link_libraries(${PROJECT_NAME} openscenegraph::osgdb_bmp)
        endif()
        if("${definition_key}" STREQUAL "WITH_JPEG")
            target_link_libraries(${PROJECT_NAME} openscenegraph::osgdb_jpeg)
        endif()
        if("${definition_key}" STREQUAL "WITH_JASPER")
            target_link_libraries(${PROJECT_NAME} openscenegraph::osgdb_jp2)
        endif()
        if("${definition_key}" STREQUAL "WITH_OPENEXR")
            target_link_libraries(${PROJECT_NAME} openscenegraph::osgdb_exr)
        endif()
        if("${definition_key}" STREQUAL "WITH_GIF")
            target_link_libraries(${PROJECT_NAME} openscenegraph::osgdb_gif)
        endif()
        if("${definition_key}" STREQUAL "WITH_PNG")
            target_link_libraries(${PROJECT_NAME} openscenegraph::osgdb_png)
        endif()
        if("${definition_key}" STREQUAL "WITH_TIFF")
            target_link_libraries(${PROJECT_NAME} openscenegraph::osgdb_tiff)
        endif()
        if("${definition_key}" STREQUAL "WITH_GDAL")
            target_link_libraries(${PROJECT_NAME} openscenegraph::osgdb_gdal)
        endif()
        if("${definition_key}" STREQUAL "WITH_GTA")
            target_link_libraries(${PROJECT_NAME} openscenegraph::osgdb_gta)
        endif()
        if("${definition_key}" STREQUAL "WITH_DCMTK")
            target_link_libraries(${PROJECT_NAME} openscenegraph::osgdb_dicom)
        endif()
        if("${definition_key}" STREQUAL "WITH_CURL")
            target_link_libraries(${PROJECT_NAME} openscenegraph::osgdb_curl)
        endif()
        if("${definition_key}" STREQUAL "WITH_ZLIB")
            target_link_libraries(${PROJECT_NAME} openscenegraph::osgdb_gz)
        endif()
        if("${definition_key}" STREQUAL "WITH_FREETYPE")
            target_link_libraries(${PROJECT_NAME} openscenegraph::osgdb_freetype)
        endif()
        if("${definition_key}" STREQUAL "WITH_IMAGEIO")
            target_link_libraries(${PROJECT_NAME} openscenegraph::osgdb_imageio)
        endif()
    endif()
endforeach()
